package zero.tech.games.dn.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import zero.tech.core.ZeroContext;
import zero.tech.core.data.KeyVal;
import zero.tech.games.common.TUserAccountUtil;
import zero.tech.games.common.WaterMode;
import zero.tech.games.dn.data.DnConfig;
import zero.tech.games.dn.data.DnGameBase;
import zero.tech.games.dn.data.DnRoomCache;
import zero.tech.games.dn.data.DnTimer;
import zero.tech.games.dn.data.entity.DnMultiple;
import zero.tech.games.dn.data.entity.PlayerData;
import zero.tech.games.dn.service.api.IDnService;
import zero.tech.games.entity.games.Club;
import zero.tech.games.entity.user.TUserAccount;
import zero.tech.games.logic.common.Room;
import zero.tech.games.logic.data.RoomStatus;
import zero.tech.games.logic.service.api.ILogicService;
import zero.tech.games.logic.util.LogicUtil;
import zero.tech.wsnet.controller.LogicController;

public class RoomUtil {

	public static void checkGameStart(Room room) {
		IDnService dnService=ZeroContext.getInstance(IDnService.class);
		DnTimer dnTimer=(DnTimer) room.getTimer();
		List<LogicController> readyPeopers=LogicUtil.readyPeopers(room);
		Map<Integer,LogicController> controllersWithoutWatcher=room.getControllersWithoutWatcherAndTaotai();
		if(readyPeopers.size()==controllersWithoutWatcher.size()&&readyPeopers.size()>=2) {
			dnService.gameStart(room);
		}else {
			int room_status=room.getRoom_status();
			if(room_status==RoomStatus.WAIT_NEXT_ROUND) {
				DnConfig dnConfig=(DnConfig) room.getGameConfig();
				ILogicService logicService=ZeroContext.getInstance(ILogicService.class);
				//自动准备
				long now=System.currentTimeMillis();
				long lastBeginToReadyTime=dnTimer.lastBeginToReadyTime;
				List<LogicController> notReadyPeopers=LogicUtil.notReadyPeopers(room);
				if(now-lastBeginToReadyTime>=dnConfig.getResult_wait_time()*1000) {
					for(LogicController notReadyPeople:notReadyPeopers) {
						if(!room.isWatcher(notReadyPeople.getId())) {
							logicService.reqReady(room,notReadyPeople);
						}
					}
				}
			}
		}
	}

	public static void checkQiangzhuangToYazhu(Room room) {
		DnGameBase dnGameBase = (DnGameBase) room.getGameBase();
		Map<Integer,PlayerData> playerDatas=dnGameBase.getPlayers();
		boolean allqiangzhuang=true;
		for(PlayerData playerData :playerDatas.values()) {
			if(playerData.getQiangzhuang_multiple()==null) {
				allqiangzhuang=false;
				break;
			}
		}
		if(allqiangzhuang) {
			//确认庄家
			confirmBankerByQiangzhuang(room);
			IDnService dnService=ZeroContext.getInstance(IDnService.class);	
			dnService.changeToYazhu(room);
		}
	}

	public static void checkLunzhuangToYazhu(Room room, Integer banker_uid) {
		DnGameBase dnGameBase = (DnGameBase) room.getGameBase();
		Map<Integer,PlayerData> playerDatas=dnGameBase.getPlayers();
		if(banker_uid==null) {
			//随机确认庄家
			List<Integer> uids=new ArrayList<>();
			uids.addAll(playerDatas.keySet());
			Collections.shuffle(uids);
			banker_uid=uids.get(0);
			dnGameBase.setBanker_uid(banker_uid);
			System.out.println("庄家uid:"+banker_uid);
		}else {
			dnGameBase.setBanker_uid(banker_uid);
			System.out.println("庄家uid:"+banker_uid);
		}
		for(LogicController logicController:room.getControllersWithoutWatcherAndTaotai().values()) {
			PlayerData playerData =playerDatas.get(logicController.getId());
			playerData.setQiangzhuang_max_multiple(1);//轮庄默认最大抢1倍
			playerData.setQiangzhuang_multiple(1);//轮庄默认抢1倍
		}
		IDnService dnService=ZeroContext.getInstance(IDnService.class);	
		dnService.changeToYazhu(room);
	}
	
	private static void confirmBankerByQiangzhuang(Room room) {
		DnGameBase dnGameBase = (DnGameBase) room.getGameBase();
		List<KeyVal<Integer, Integer>> list=new ArrayList<>();
		Map<Integer, PlayerData> map=dnGameBase.getPlayers();
		for(PlayerData playerData:map.values()) {
			if(!playerData.getBuqiang()) {
				list.add(new KeyVal<Integer, Integer>(playerData.getUid(), playerData.getQiangzhuang_multiple()));
			}
		}
		
		if(list.isEmpty()) {
			//所有人都不抢,随机一个作为庄家
			List<Integer> uids=new ArrayList<>();
			uids.addAll(map.keySet());
			Collections.shuffle(uids);
			int banker_uid=uids.get(0);
			dnGameBase.setBanker_uid(banker_uid);
			System.out.println("庄家uid:"+banker_uid);
		}else if(list.size()==1){
			int banker_uid=list.get(0).getK();
			dnGameBase.setBanker_uid(banker_uid);
			System.out.println("庄家uid:"+banker_uid);
		}else {
			int max=0;
			for(KeyVal<Integer, Integer> kv:list) {
				if(kv.getV()>=max) {
					max=kv.getV();
				}
			}
			
			List<Integer> uids=new ArrayList<>();
			for(KeyVal<Integer, Integer> kv:list) {
				if(kv.getV()==max) {
					uids.add(kv.getK());
				}
			}
			Collections.shuffle(uids);
			int banker_uid=uids.get(0);
			dnGameBase.setBanker_uid(banker_uid);
			System.out.println("庄家uid:"+banker_uid);
		}
		
	}

	public static void checkYazhuToLiangpai(Room room) {
		//所有非庄家的玩家都压注了
		DnGameBase dnGameBase = (DnGameBase) room.getGameBase();
		int banker_uid=dnGameBase.getBanker_uid();
		Map<Integer,PlayerData> playerDatas=dnGameBase.getPlayers();
		boolean allyazhu=true;
		for(PlayerData playerData :playerDatas.values()) {
			int uid=playerData.getUid();
			if(uid!=banker_uid) {
				if(playerData.getYazhu_multiple()==null) {
					allyazhu=false;
					break;
				}
			}
		}
		if(allyazhu) {
			//检查所有玩家都已经压注了
			IDnService dnService=ZeroContext.getInstance(IDnService.class);	
			dnService.changeToLiangpai(room);
		}
	}

	public static void checkLiangpaiToGameOver(Room room) {
		DnGameBase dnGameBase = (DnGameBase) room.getGameBase();
		Map<Integer,PlayerData> playerDatas=dnGameBase.getPlayers();
		boolean allliangpai=true;
		for(PlayerData playerData :playerDatas.values()) {
			if(playerData.getLiangpai()==null) {
				allliangpai=false;
				break;
			}
		}
		if(allliangpai) {
			//检查所有玩家都已经亮牌了
			IDnService dnService=ZeroContext.getInstance(IDnService.class);	
			dnService.gameOver(room);
		}
	}

	public static int calQiangzhuangMaxMultiple(Room room, LogicController logicController) {
		DnConfig dnConfig = (DnConfig) room.getGameConfig();
		int can_max=dnConfig.getQiangzhuang_max_multiple();
		if(room.getClub()==null) {
			return can_max;
		}
		long gold=logicController.getGold();
		long tmp=gold;
		if(dnConfig.getWater_mode()==1) {
			//扣除AA房费
			long water_num=dnConfig.getWater_num();
			tmp-=water_num;
		}
		int size=room.getControllersWithoutWatcherAndTaotai().size();
		DnRoomCache dnRoomCache=(DnRoomCache) room.getRoomCache();
		DnMultiple dnMultiple=dnRoomCache.getDn_multiple();
		int max=dnMultiple.getMaxMultile();
		int qiangzhuang=(int) (tmp/(max*(size-1)*5*dnConfig.getBase_score()));
		qiangzhuang=qiangzhuang>=can_max?can_max:qiangzhuang;
		qiangzhuang=qiangzhuang<=1?1:qiangzhuang;
		return qiangzhuang;
	}

	public static int calYazhuMaxMultiple(Room room, LogicController logicController) {
		if(room.getClub()==null) {
			return 5;
		}
		DnConfig dnConfig = (DnConfig) room.getGameConfig();
		long gold=logicController.getGold();
		long tmp=gold;
		if(dnConfig.getWater_mode()==1) {
			//扣除AA房费
			long water_num=dnConfig.getWater_num();
			tmp-=water_num;
		}
		DnGameBase dnGameBase = (DnGameBase) room.getGameBase();
		int banker_uid=dnGameBase.getBanker_uid();
		PlayerData banker_data=dnGameBase.getPlayers().get(banker_uid);
		int banker_qiangzhuang_multiple=banker_data.getQiangzhuang_multiple();
		int size=1;
		DnRoomCache dnRoomCache=(DnRoomCache) room.getRoomCache();
		DnMultiple dnMultiple=dnRoomCache.getDn_multiple();
		int max=dnMultiple.getMaxMultile();
		int yazhu=(int) (tmp/(max*size*banker_qiangzhuang_multiple*dnConfig.getBase_score()));
		yazhu=yazhu>=5?5:yazhu;
		yazhu=yazhu<=1?1:yazhu;
		return yazhu;
	}

	public static Boolean checkMoneyEnoughNextRound(Room room, LogicController logicController) {
		Club club=room.getClub();
		if(club!=null) {
			DnConfig dnConfig = (DnConfig) room.getGameConfig();
			long gold=logicController.getGold();
			int room_status=room.getRoom_status();
			if(room_status==RoomStatus.DEFAULT) {
				Long admission_fee=dnConfig.getAdmission_fee();
				if(gold>=admission_fee) {
					return true;
				}
			}else {
				DnRoomCache dnRoomCache = (DnRoomCache) room.getRoomCache();
				DnMultiple dnMultiple=dnRoomCache.getDn_multiple();
				int water_mode=dnConfig.getWater_mode();
				long need_aa=0L;
				if(water_mode==WaterMode.AA) {
					//需要预留AA抽水的钱
					need_aa=dnConfig.getWater_num();
				}
				long need=need_aa;
				if(dnConfig.getZhuang_mode()==0) {
					need+=dnConfig.getQiangzhuang_max_multiple()*dnMultiple.getMaxMultile()*dnConfig.getBase_score();
				}
				if(gold>=need) {
					return true;
				}
			}
			return false;
		}else {
			int uid=logicController.getId();
			TUserAccountUtil userAccountUtil=ZeroContext.getInstance(TUserAccountUtil.class);
			TUserAccount userAccount=userAccountUtil.getUserAccount(uid);
			DnConfig dnConfig =(DnConfig) room.getGameConfig();
			int water_mode=dnConfig.getWater_mode();
			long water_num=dnConfig.getWater_num();
			if(water_mode==WaterMode.AA) {
				if(userAccount.getDiamond()>=water_num) {
					return true;
				}
			}
			return false;
//			return true;
		}
	}



}
